package com.fau.fm.action.index;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.OutputStream;
import java.text.ParseException;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.fau.fm.action.PublicAction;
import com.fau.fm.entity.MedicalAdmin;
import com.fau.fm.entity.MedicalPerformanceSalary;
import com.fau.fm.util.DateUtil;
import com.fau.fm.util.FileNameEncoderUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;

/**
 * @desc 绩效薪资设置Action
 * @author dangjh
 * @datetime 2014年11月20日,上午11:53:13
 */
@Controller
@Scope("prototype")
@ParentPackage("default")
@Namespace("/")
public class PerformanceSalaryAction extends PublicAction implements ModelDriven<MedicalPerformanceSalary> {

	private static final long serialVersionUID = -6380678580556666073L;
	
	private static final Logger log = Logger.getLogger(PerformanceSalaryAction.class);
	
	private MedicalPerformanceSalary performance = new MedicalPerformanceSalary();
	
	/**
	 * @desc 绩效薪资设置页面
	 * @return String
	 * @author dangjh
	 * @datetime 2014年11月19日,下午9:36:03
	 */
	@Action(value = "performance_salary_init", results = {@Result(location = "performance_salary_init.jsp")})
	public String performanceSalaryInit() {
		return SUCCESS;
	}
	
	/**
	 * @desc 话务员绩效薪资设置
	 * @return String
	 * @author dangjh
	 * @datetime 2014年11月20日,下午1:29:55
	 */
	@Action(value = "call_employee_salary", results = {@Result(name = SUCCESS, location = "call_employee_salary.jsp")})
	public String callEmployeeSalary() {
		return SUCCESS;
	}
	
	/**
	 * @desc 核单员绩效薪资设置
	 * @return String
	 * @author dangjh
	 * @datetime 2014年11月20日,下午1:29:55
	 */
	@Action(value = "nuclear_employee_salary", results = {@Result(name = SUCCESS, location = "nuclear_employee_salary.jsp")})
	public String nuclearEmployeeSalary() {
		return SUCCESS;
	}
	
	/**
	 * @desc 话务组长、核单组长绩效薪资设置
	 * @return String
	 * @author dangjh
	 * @datetime 2014年11月20日,下午1:29:55
	 */
	@Action(value = "group_leader_salary", results = {@Result(name = SUCCESS, location = "group_leader_salary.jsp")})
	public String groupLeaderSalary() {
		return SUCCESS;
	}
	
	/**
	 * @desc 话务员绩效薪资数据列表
	 * @return String
	 * @author dangjh
	 * @datetime 2014年11月19日,下午9:39:31
	 */
	@Action(value = "performance_salary_view")
	public String performanceSalaryView() {
		HttpServletResponse response = ServletActionContext.getResponse();
		this.map = ActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		this.pageBean = this.baseAllService.getPerformanceService().findCallEmployeeByPage(map, performance, admin);
		this.map.clear();
		map.put("total", this.pageBean.getTotalRow());
		map.put("data", this.pageBean.getResult());
		formatJSON(response, this.map);
		return null;
	}
	
	/**
	 * @desc 核单员绩效薪资数据列表
	 * @return String
	 * @author dangjh
	 * @datetime 2014年11月19日,下午9:39:31
	 */
	@Action(value = "nuclear_performance_salary_view")
	public String nuclearPerformanceSalaryView() {
		HttpServletResponse response = ServletActionContext.getResponse();
		this.map = ActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		this.pageBean = this.baseAllService.getPerformanceService().findNuclearEmployeeByPage(map, performance, admin);
		this.map.clear();
		map.put("total", this.pageBean.getTotalRow());
		map.put("data", this.pageBean.getResult());
		formatJSON(response, this.map);
		return null;
	}
	
	/**
	 * @desc 话务组长、核单组长绩效薪资数据列表
	 * @return String
	 * @author dangjh
	 * @datetime 2014年11月19日,下午9:39:31
	 */
	@Action(value = "group_leader_salary_view")
	public String groupLeaderSalaryView() {
		HttpServletResponse response = ServletActionContext.getResponse();
		this.map = ActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		this.pageBean = this.baseAllService.getPerformanceService().findGroupByPage(map, performance, admin);
		this.map.clear();
		map.put("total", this.pageBean.getTotalRow());
		map.put("data", this.pageBean.getResult());
		formatJSON(response, this.map);
		return null;
	}
	
	/**
	 * @desc 打开话务员绩效薪资设置界面
	 * @return String
	 * @author dangjh
	 * @datetime 2014年11月20日,上午10:50:28
	 */
	@Action(value = "call_performance_salary_add", results = {@Result(name = SUCCESS, location = "call_performance_salary_add.jsp")})
	public String callPerformanceSalaryAdd() {
		return SUCCESS;
	}
	
	/**
	 * @desc 打开核单员绩效薪资设置界面
	 * @return String
	 * @author dangjh
	 * @datetime 2014年11月20日,上午10:50:28
	 */
	@Action(value = "nuclear_performance_salary_add", results = {@Result(name = SUCCESS, location = "nuclear_performance_salary_add.jsp")})
	public String nuclearPerformanceSalaryAdd() {
		return SUCCESS;
	}
	
	/**
	 * @desc 打开组长绩效薪资设置界面
	 * @return String
	 * @author dangjh
	 * @datetime 2014年11月20日,上午10:50:28
	 */
	@Action(value = "group_performance_salary_add", results = {@Result(name = SUCCESS, location = "group_performance_salary_add.jsp")})
	public String groupPerformanceSalaryAdd() {
		return SUCCESS;
	}
	
	/**
	 * @desc 保存绩效薪资设置
	 * @return String
	 * @author dangjh
	 * @datetime 2014年11月20日,上午10:52:46
	 */
	@Action(value = "performance_salary_save")
	public String performanceSalarySave() {
		HttpServletResponse response = ServletActionContext.getResponse();
		this.map = new HashMap<String, Object>();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		performance.setEid(admin.getEnterpriseId());
		boolean flag = this.baseAllService.getPerformanceService().save(performance);
		if(flag) {
			this.map.put("success", true);
		} else {
			this.map.put("success", false);
		}
		formatJSON(response, this.map);
		return null;
	}
	
	/**
	 * @desc 打开话务员绩效薪资编辑界面
	 * @return String
	 * @author dangjh
	 * @datetime 2014年11月20日,下午7:40:55
	 */
	@Action(value="call_performance_salary_edit", results = {@Result(name = SUCCESS, location = "call_performance_salary_edit.jsp")})
	public String callPerformanceSalaryEdit() {
		return SUCCESS;
	}
	
	/**
	 * @desc 打开核单员绩效薪资编辑界面
	 * @return String
	 * @author dangjh
	 * @datetime 2014年11月20日,下午7:40:55
	 */
	@Action(value="nuclear_performance_salary_edit", results = {@Result(name = SUCCESS, location = "nuclear_performance_salary_edit.jsp")})
	public String nuclearPerformanceSalaryEdit() {
		return SUCCESS;
	}
	
	/**
	 * @desc 打开组长绩效薪资编辑界面
	 * @return String
	 * @author dangjh
	 * @datetime 2014年11月20日,下午7:40:55
	 */
	@Action(value="group_performance_salary_edit", results = {@Result(name = SUCCESS, location = "group_performance_salary_edit.jsp")})
	public String groupPerformanceSalaryEdit() {
		return SUCCESS;
	}
	
	/**
	 * @desc 保存编辑的绩效薪资设置
	 * @return String
	 * @author dangjh
	 * @datetime 2014年11月20日,上午10:52:46
	 */
	@Action(value = "performance_salary_save_edit")
	public String performanceSalarySaveEdit() {
		HttpServletResponse response = ServletActionContext.getResponse();
		this.map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(performance.getId())) {
			boolean flag = this.baseAllService.getPerformanceService().saveEdit(performance);
			if(flag) {
				this.map.put("success", true);
			} else {
				this.map.put("success", false);
			}
		} else {
			map.put("success", false);
		}
		formatJSON(response, this.map);
		return null;
	}
	
	/**
	 * @desc 删除绩效薪资信息
	 * @return String
	 * @author dangjh
	 * @datetime 2014年11月20日,下午5:58:39
	 */
	@Action(value = "performance_salary_delete")
	public String delPerformanceSalary() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		if(StringUtils.isNotBlank(performance.getId())) {
			this.baseAllService.getPerformanceService().delete(performance);
			map.put("success", true);
		} else {
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 查询药品类别树形集合
	 * @return
	 * @author dangjh
	 * @datetime 2014年11月11日,下午4:44:27
	 */
	@Action(value = "performance_medicine_sort")
	public String findMedicineSort() {
		HttpServletResponse response = ServletActionContext.getResponse();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		list = this.baseAllService.getPerformanceService().findMedicineSortList(admin);
		formatJSON(response, list);
		return null;
	}
	
	/**
	 * @desc 判断当前药品类别或职位薪资是否已经存在
	 * @return
	 * @author dangjh
	 * @datetime 2014年11月20日,下午8:29:19
	 */
	@Action(value = "salary_include_medicine")
	public String salaryIncludeMedicine() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		boolean flag = this.baseAllService.getPerformanceService().includeMedicine(performance, admin);
		if(flag) {
			map.put("success", false);
		} else {
			map.put("success", true);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 进入员工工资表页面
	 * @return String
	 * @author liy
	 * @datetime 2014年11月15日,下午3:00:03
	 */
	@Action(value = "staff_salary_table", results = {@Result(location = "staff_salary_table.jsp")})
	public String staffSalaryTable(){
		return SUCCESS;
	}
	
	/**
	 * @desc 生产员工薪资界面
	 * @return String
	 * @author dangjh
	 * @datetime 2014年11月27日 下午12:16:58
	 */
	@Action(value = "staff_salary_add", results = {@Result(location = "staff_salary_add.jsp")})
	public String staffSalaryAdd() {
		return SUCCESS;
	}
	
	/**
	 * @desc 保存计算员工薪资
	 * @return
	 * @author dangjh
	 * @datetime 2014年11月27日 下午12:18:06
	 */
	@Action(value = "staff_salary_save")
	public String staffSalarySave() {
		HttpServletResponse response = ServletActionContext.getResponse();
		map = new HashMap<String, Object>();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		performance.setHospitalId(admin.getEnterpriseId());
		boolean flag = this.baseAllService.getPerformanceService().saveCalculatingSalaries(performance);
		if(flag) {
			map.put("success", true);
		} else {
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	/**
	 * @desc 员工工资表数据展示
	 * @return null
	 * @author dangjh
	 * @datetime 2014年11月27日 下午12:02:36
	 */
	@Action(value = "staff_salary_table_view")
	public String staffSalaryTableView() {
		HttpServletResponse response = ServletActionContext.getResponse();
		this.map = ActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		performance.setHospitalId(admin.getEnterpriseId());
		try {
			this.pageBean = this.baseAllService.getPerformanceService().findAllStaffSalary(map, performance);
		} catch (ParseException e) {
			log.error(e.getMessage());
		}
		this.map.clear();
		this.map.put("total", this.pageBean.getTotalRow());
		this.map.put("data", this.pageBean.getResult());
		formatJSON(response, this.map);
		return null;
	}
	
	/**
	 * @desc 导出已选员工工资
	 * @return null
	 * @author dangjh
	 * @datetime 2014年11月27日 上午11:45:31
	 */
	@Action(value = "staff_export_salary")
	public String exportSelected() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			MedicalAdmin user = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			performance.setHospitalId(user.getEnterpriseId());
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"员工工资信息表"+DateUtil.getDate()+".xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			Workbook workbook = this.baseAllService.getPerformanceService().exportSelected(performance);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @desc 导出所有员工工资
	 * @return null
	 * @author dangjh
	 * @datetime 2014年11月27日 上午11:45:31
	 */
	@Action(value = "staff_export_all_salary")
	public String exportAll() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			MedicalAdmin user = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			performance.setHospitalId(user.getEnterpriseId());
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"员工工资信息表"+DateUtil.getDate()+".xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			Workbook workbook = this.baseAllService.getPerformanceService().exportAll(performance);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @desc 下载话务员绩效模板
	 * @return null
	 * @author dangjh
	 * @datetime 2014年11月27日 上午11:45:31
	 */
	@Action(value = "call_staff_down_salary")
	public String download() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			MedicalAdmin user = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"话务员绩效薪资.xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			Workbook workbook = this.baseAllService.getPerformanceService().downloadMedicine(user);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @desc 打开批量导入页面
	 * @return SUCCESS
	 * @author kangyj
	 * @datetime 2014年10月15日,下午10:19:19
	 */
	@Action(value = "call_salary_import_add", results = {@Result(location = "call_salary_import_add.jsp")})
	public String importAdd(){
		return SUCCESS;
	}
	
	/**
	 * @desc 导入保存
	 * @return null
	 * @author dangjh
	 * @datetime 2014年10月22日,上午9:53:43
	 */
	@Action(value = "call_salary_import_save")
	public String importSave(){
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		map = new HashMap<String, Object>();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		File file = this.getImportFile(request, response);
		try {
			this.baseAllService.getPerformanceService().importCallSalary(admin, file);
			map.put("success", true);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("success", false);
		}
		formatJSON(response, map);
		return null;
	}
	
	@Override
	public MedicalPerformanceSalary getModel() {
		return performance;
	}

}
